package com.tms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tms.entity.BookCategory;
import com.tms.entity.PageBean;
import com.tms.entity.Result;
import com.tms.service.BookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 对于教材的分类情况，教材类型对应相应的序号(BookCategory)表控制层
 *
 * @author makejava
 * @since 2024-12-27 01:22:06
 */
@RestController
@RequestMapping("bookCategory")
public class BookCategoryController {
    @Autowired
    private BookCategoryService bookCategoryService;


    //新增
    @PostMapping("/save")
    public Result save(@RequestBody BookCategory bookCategory) {
        return bookCategoryService.save(bookCategory) ? Result.success() : Result.error();
    }

    //修改
    @PostMapping("/update")
    public Result update(@RequestBody BookCategory bookCategory) {
        return bookCategoryService.updateById(bookCategory) ? Result.success() : Result.error();
    }

    //删除
    @DeleteMapping("/del")
    public Result del(@RequestParam String id) {
        return bookCategoryService.removeById(id) ? Result.success() : Result.error();
    }


    @GetMapping("/pageList")
    public Result pageList(@RequestBody PageBean pageBean) {
        HashMap params = pageBean.getParams();

        String categoryName = params.get("categoryName").toString();

        Page<BookCategory> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());

        LambdaQueryWrapper<BookCategory> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(categoryName) && !"null".equals(categoryName)) {
            lambdaQueryWrapper.like(BookCategory::getCategoryName, categoryName);
        }

        IPage result = bookCategoryService.pageList(page, lambdaQueryWrapper);

        return Result.success(result.getRecords());
    }

    @GetMapping("/list")
    public Result list() {
        List<BookCategory> list = bookCategoryService.list();
        return Result.success(list);
    }

}

